package com.cyf.pc;

import com.cyf.pc.network.bean.PCBusinessRsp;
import com.cyf.pc.network.bean.PCGetQrcodeReq;
import com.cyf.pc.network.bean.PCGetQrcodeRsp;
import com.cyf.pc.service.PCPayService;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api("pc端相关api")
@RequestMapping("pc")
@CrossOrigin(allowedHeaders = "x-requested-with", origins = "*")
public class PCPayHttpController {

    private static final Logger logger = LoggerFactory.getLogger(PCPayHttpController.class);

    @Autowired
    private PCPayService pcPayService;

    @ApiOperation(value = "广告支付获取二维码码串")
    @PostMapping(value = "/getAdFeeQrcode")
//    public PCGetQrcodeRsp getAdFeeQrcode(@RequestBody PCGetQrcodeReq req,
//                                         HttpServletRequest request) {
    public PCGetQrcodeRsp getAdFeeQrcode(@RequestParam String orderNo,
                                         @RequestParam int payWay,
                                         HttpServletRequest request) {
    /*    public PCGetQrcodeRsp getAdFeeQrcode(
        HttpServletRequest request) {*/
        PCGetQrcodeReq req = new PCGetQrcodeReq();
        /*req.setOrderNo("xx");
        req.setPayWay(12);*/
        req.setOrderNo(orderNo);
        req.setPayWay(payWay);
        req.setReqIp(request.getRemoteHost());
        return pcPayService.getAdFeeQrcode(req);
    }

    @ApiOperation(value = "广告支付获取二维码")
    @PostMapping(value = "/getAdFeeQrcodeImg")
    public void getAdFeeQrcodeImg(@RequestBody PCGetQrcodeReq req,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
//    public void getAdFeeQrcodeImg(@RequestParam int payWay,
//                                  @RequestParam String orderNo,
//                                  HttpServletRequest request,
//                                  HttpServletResponse response) {
//        PCGetQrcodeReq req = new PCGetQrcodeReq();
//        req.setOrderNo(orderNo);
//        req.setPayWay(payWay);
        req.setReqIp(request.getRemoteHost());
        PCGetQrcodeRsp pcGetQrcodeRsp = pcPayService.getAdFeeQrcode(req);
        if (pcGetQrcodeRsp.getResult() == PCBusinessRsp.SUCCESS) {
            try {
                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                Map map = new HashMap();
                // 内容所使用编码
                map.put(EncodeHintType.CHARACTER_SET, "gb2312");
                BitMatrix bitMatrix = multiFormatWriter.encode(pcGetQrcodeRsp.getQrcode(),
                        BarcodeFormat.QR_CODE, 200, 200, map);
                // 生成二维码
                MatrixToImageWriter.writeToStream(bitMatrix, "jpg", response.getOutputStream());
            } catch (Exception e) {
                logger.error("二维码串("+ pcGetQrcodeRsp.getQrcode() +")转二维码图片异常：{}", e);
            }
        }

    }
}
